package org.bmi.gwt.examples.echo.client;

import java.util.Date;

import org.bmi.gwt.examples.echo.shared.EchoProxy;
import org.bmi.gwt.mi.client.channel.Connection;
import org.bmi.gwt.mi.client.events.ConnectionHandler;
import org.bmi.gwt.mi.shared.BroadChannel;
import org.bmi.gwt.mi.shared.Channel;
import org.bmi.gwt.mi.shared.callbacks.BindCallback;
import org.bmi.gwt.mi.shared.exceptions.InvalidArgumentException;
import org.bmi.gwt.mi.shared.exceptions.MiException;
import org.bmi.gwt.mi.shared.exceptions.NameParseException;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;

public class EchoTest implements EntryPoint
{
	/*
	 * This example attempts to explain the very basic feature of BroadcastMi:
	 */

	@Override
	public void onModuleLoad()
	{
		/*
		 * A connection must be open, and a handler implementing
		 * ConnectionHandler interface should be implemented.
		 * 
		 * All the code concerning queue and MI should be contained in
		 * onConnectionBegin() or called from this method.
		 * 
		 * The method createConnection in fact returns a connection, anyway it's
		 * recommended to use the connection object which is the argument of the
		 * implemented onConnectionBegin() function.
		 */
		Connection.createConnection(new ConnectionHandler()
		{
			@Override
			public void onException(Exception e)
			{
				GWT.log("an error occurred: ", e);
			}

			@Override
			public void onConnectionBegin(final Connection connection)
			{
				/*
				 * The method onConnectionBegin() is implemented in the way that
				 * all the code using the connection and queue features must be
				 * here.
				 * 
				 * Are then created 2 elements: a queue and a queue pool. the
				 * first is bound with current client; the second being a pool,
				 * is used to send call requests.
				 * 
				 * While creating a proxy from the pool, the constructor is
				 * invoked. This will create in this client which is bound to
				 * the queue the instantiation of the target class of that
				 * proxy.
				 * 
				 * The echo method is invoked from its proxy, providing current
				 * date and the value from a text field. It will provoke an
				 * execution of the real object, in the client.
				 */
				try
				{
					Channel qListen = connection.getChannel("org.bmi.gwt.mi.test.echo");

					qListen.bind(new BindCallback()
					{
						@Override
						public void onError(MiException error)
						{
							GWT.log("An error occurred: " + error);
						}

						@Override
						public void onBound(Channel channel)
						{
							try
							{
								BroadChannel qSend = connection.getChannel("org.bmi.gwt.mi.test.echo");
								/*
								 * The proxy to a new object must be defined
								 * once this peer is bound to the queue, in this
								 * way the constructor invocation doesn't
								 * expire, since out of a transaction every
								 * request expires immediately.
								 */
								final EchoProxy proxy = qSend.proxyToNewObject(EchoProxy.class, "A string:");

								final TextBox echoBox = new TextBox();
								Button sendButton = new Button("Send", new ClickHandler()
								{
									@Override
									public void onClick(ClickEvent event)
									{
										proxy.echo(new Date(), echoBox.getValue());
									}
								});

								RootPanel.get().add(echoBox);
								RootPanel.get().add(sendButton);
							}
							catch (NameParseException e)
							{
								GWT.log("", e);
							}
							catch (InvalidArgumentException e)
							{
								GWT.log("", e);
							}
						}
					});
				}
				catch (NameParseException e)
				{
					GWT.log("", e);
				}
			}
		});
	}
}
